<template>
  <div>
    <span>管理员信息</span>
    <el-divider></el-divider>
    <span>管理员ID:{{ users.id }}</span>
    <el-divider></el-divider>
    <span>账号:{{ users.account }}</span>
    <el-divider></el-divider>
    <span>用户组:{{ users.userGroup }}</span>
    <el-divider></el-divider>
    <span>创建时间:{{ users.ctime }}</span>
    <el-divider></el-divider>
    <p style="display:flex;line-height:178px;">管理员头像:
      <el-upload
        class="avatar-uploader"
        action="http://127.0.0.1:5000/users/avatar_upload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :data="updata"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </p>
    <el-divider></el-divider>
  </div>
</template>

<script>
import { username } from "@/apis/users";
import { bus} from '../utils/utils'
export default {
  data() {
    return {
      users: {},
      imageUrl: "",
      updata:{id:localStorage.user}
    };
  },
  created() {
    let users = JSON.parse(window.localStorage.getItem("user"));
    username({ id: users }).then((res) => {
      this.users = res.data.accountInfo;
      let d = new Date(this.users.ctime); //时间格式从国际时间转换为时区时间
      this.users.ctime =
        d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
    });
  },
  methods: {
    //上传头像后的回调函数
    handleAvatarSuccess(res, file) {
     this.$message({
          message:res.msg,
          type: 'success'
        });
       this.imageUrl = URL.createObjectURL(file.raw);
    bus.$emit('headrefrent',this.imageUrl)
   },
  },
};
</script>

<style lang="less" scoped>
img {
  width: 80px;
  height: 80px;
  border-radius: 40px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader{
  margin-left:30px;
  height:178px;
  border:1px dotted #ccc;
  border-radius: 40px;
}
</style>